<template>
  <Dialog v-model="dialogVisible" :title="`${classInfo?.className || ''}班级学生管理`" width="1000px">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="学号" prop="studentNo">
        <el-input
          v-model="queryParams.studentNo"
          placeholder="请输入学号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" @click="openImportForm"><Icon icon="ep:upload" class="mr-5px" /> 导入</el-button>
      </el-form-item>
    </el-form>

    <!-- 学生列表 -->
    <el-table v-loading="loading" :data="studentList" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="学生ID" align="center" prop="studentId" />
      <el-table-column label="学号" align="center" prop="studentNo" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column
        label="加入时间"
        align="center"
        prop="joinTime"
        :formatter="dateFormatter"
        width="180px"
      />
    </el-table>
    
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getStudentList"
    />

    <template #footer>
      <el-button @click="dialogVisible = false">关 闭</el-button>
    </template>
  </Dialog>

  <!-- 学生导入弹窗 -->
  <StudentImportForm ref="importFormRef" @success="getStudentList" />
</template>

<script lang="ts" setup>
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import { ExamClassStudentApi, ExamClassStudentVO } from '@/api/system/examclassstudent'
import { ExamClassVO } from '@/api/system/examclass'
import StudentImportForm from './StudentImportForm.vue'

defineOptions({ name: 'StudentListDialog' })

const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const loading = ref(false) // 列表的加载中
const studentList = ref<ExamClassStudentVO[]>([]) // 学生列表数据
const total = ref(0) // 列表的总页数
const classInfo = ref<ExamClassVO>() // 当前班级信息
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  studentNo: undefined,
  studentName: undefined,
  phone: undefined,
})
const queryFormRef = ref() // 搜索的表单
const importFormRef = ref() // 导入表单的引用

/** 打开弹窗 */
const open = (classData: ExamClassVO) => {
  dialogVisible.value = true
  classInfo.value = classData
  resetQuery()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 查询学生列表 */
const getStudentList = async () => {
  if (!classInfo.value?.id) return
  
  loading.value = true
  try {
    const data = await ExamClassStudentApi.getStudentListByClass(classInfo.value.id)
    // 根据搜索条件过滤数据
    let filteredData = data || []
    if (queryParams.studentNo) {
      filteredData = filteredData.filter(item => 
        item.studentNo?.includes(queryParams.studentNo)
      )
    }
    if (queryParams.studentName) {
      filteredData = filteredData.filter(item => 
        item.studentName?.includes(queryParams.studentName)
      )
    }
    if (queryParams.phone) {
      filteredData = filteredData.filter(item => 
        item.phone?.includes(queryParams.phone)
      )
    }
    
    // 分页处理
    total.value = filteredData.length
    const startIndex = (queryParams.pageNo - 1) * queryParams.pageSize
    const endIndex = startIndex + queryParams.pageSize
    studentList.value = filteredData.slice(startIndex, endIndex)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getStudentList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  queryParams.pageNo = 1
  queryParams.pageSize = 10
  queryParams.studentNo = undefined
  queryParams.studentName = undefined
  queryParams.phone = undefined
  getStudentList()
}

/** 打开导入弹窗 */
const openImportForm = () => {
  importFormRef.value?.open(classInfo.value)
}
</script>
